package com.sihan.swvp.action.jc.bzzy;



import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.jc.JcZyType;
import com.sihan.swvp.entity.jc.bzzy.BzzyZygc;
import com.sihan.swvp.service.jc.JcZyTypeService;
import com.sihan.swvp.service.jc.bzzy.BzzyMajorService;
import com.sihan.swvp.service.jc.bzzy.BzzyZygcService;
import com.sihan.swvp.service.sys.LogService;

@Controller
@RequestMapping("/bzzy/BzzyZygcAction")
public class BzzyZygcAction extends BaseAction{

	@Autowired
	private BzzyZygcService bzzyZygcService;
	@Autowired
	private BzzyMajorService bzzyMajorService;
	@Autowired
	private JcZyTypeService zyTypeService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping("/getBzzyZygc")
	public Object getBzzyZygc(@Param("zytypeId")String zytypeId,@Param("zygcName")String zygcName){
		try {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("zytypeId", zytypeId);
			params.put("zygcName", zygcName);
			return bzzyZygcService.getBzzyZygc(params);
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常"); 
		}
		
	}
	/**
	 * 查询所属专业
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getBzzyMajor")
	public Object getBzzyMajor(@Param("bzzyName")String bzzyName){
		try {
			Map<String, Object> param=new HashMap();
			param.put("bzzyName", bzzyName);
			return bzzyMajorService.getBzzyMajor(param,null,null);
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常"); 
		}
	}
	
	@ResponseBody
	@RequestMapping("/addBzzyZygc")
	public Object addBzzyZygc(BzzyZygc bean){
		try {
			Map<String, Object> validate = bzzyZygcService.getBzzyZygcValidate(bean);
			int nameNubmer=Integer.parseInt(validate.get("nameNubmer").toString());
			if(nameNubmer !=0){
				return failMsg("该过程名称已存在");
			}
			bean.setUpuser(getLoginUser().getId());
			logService.addUserLog("添加作业过程", "作业过程", getLoginUser().getId(), "添加作业过程："+bean.getZygcName(),getRemoteAddr(),1,0);
			bzzyZygcService.addBzzyZygc(bean);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加作业过程", "作业过程", getLoginUser().getId(), "添加作业过程："+bean.getZygcName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/updateBzzyZygc")
	public Object updateBzzyZygc(BzzyZygc bean){
		try {
			Map<String, Object> validate=bzzyZygcService.getBzzyZygcValidate(bean);
			int nameNubmer=Integer.parseInt(validate.get("nameNubmer").toString());
			if(nameNubmer !=0){
				return failMsg("过程名称已存在");
			}
			bean.setUpuser(getLoginUser().getId());
			bzzyZygcService.updateBzzyZygc(bean);
			logService.addUserLog("修改作业过程", "作业过程", getLoginUser().getId(), "修改作业过程："+bean.getZygcName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改作业过程", "作业过程", getLoginUser().getId(), "修改作业过程："+bean.getZygcName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteBzzyZygc")
	public Object deleteBzzyZygc(@Param("id")Integer id,String zygcName){
		try {
			bzzyZygcService.deleteBzzyZygc(id);
			logService.addUserLog("删除作业过程", "作业过程", getLoginUser().getId(), "删除作业过程："+zygcName,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除作业过程", "作业过程", getLoginUser().getId(), "删除作业过程："+zygcName,getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	/**
	 * 排序
	 * @param idArr
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateJcBzjsbzzySort")
	public Object updateJcBzjsbzzySort(@Param("idArr")String idArr){
		try {
			BzzyZygc bean=new BzzyZygc();
			String[] ids=idArr.split(",");
			for (int i = 0; i < ids.length; i++) {
				bean.setId(Integer.parseInt(ids[i].toString()));
				bean.setSort(i+1);
				bzzyZygcService.updateBzzyZygcSort(bean);	
			}
			
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	}
	

	/**
	 * 查询作业类型
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJcZyType")
	public Object getJcZyType(){
		return zyTypeService.getJcZyType();
	}
	
	/**
	 * 添加作业类型
	 * @param bean
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addJcZyType")
	public Object addJcZyType(JcZyType bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			Integer nameNumber=zyTypeService.getJcZyTypeValidate(null,bean.getName(),null);
			Integer codeNumber=zyTypeService.getJcZyTypeValidate(null,null,bean.getCode());
			if(nameNumber !=0){
				return failMsg("类型名称已存在");
			}else if(codeNumber !=0){
				return failMsg("类型编码已存在");
			}
			zyTypeService.addJcZyType(bean);
			logService.addUserLog("添加作业类型", "作业类型", getLoginUser().getId(), "添加作业类型："+bean.getName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加作业类型", "作业类型", getLoginUser().getId(), "添加作业类型："+bean.getName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	/**
	 * 修改作业类型
	 * @param bean
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateJcZyType")
	public Object updateJcZyType(JcZyType bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			Integer nameNumber=zyTypeService.getJcZyTypeValidate(bean.getId(),bean.getName(),null);
			Integer codeNumber=zyTypeService.getJcZyTypeValidate(bean.getId(),null,bean.getCode());
			if(nameNumber !=0){
				return failMsg("类型名称已存在");
			}else if(codeNumber !=0){
				return failMsg("类型编码已存在");
			}
			zyTypeService.updateJcZyType(bean);
			logService.addUserLog("修改作业类型", "作业类型", getLoginUser().getId(), "修改作业类型："+bean.getName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改作业类型", "作业类型", getLoginUser().getId(), "修改作业类型："+bean.getName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	/**
	 * 删除作业类型
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteJcZyType")
	public Object deleteJcZyType(@Param("id")Integer id,String name){
		try {
			zyTypeService.deleteJcZyType(id);
			logService.addUserLog("删除作业类型", "作业类型", getLoginUser().getId(), "删除作业类型："+name,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除作业类型", "作业类型", getLoginUser().getId(), "删除作业类型："+name,getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
}
